/***
 * Excerpted from "OpenGL ES for Android",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information.
***/
package openGl;

import android.opengl.GLES20;

import java.nio.FloatBuffer;

import camera.OpenGl.utils.ByteBufferUtils;
import openGl.utils.ColorShaderProgram;
import openGl.utils.TexureShaderProgram;

import static android.opengl.GLES20.glDrawArrays;

public class Table {
    private ColorShaderProgram colorShaderProgram;
    private TexureShaderProgram texureShaderProgram;

    
    private static final float[] VERTEX_DATA = {
        // Order of coordinates: X, Y,

        // Triangle Fan
            0f,    0f, 0.5f, 0.5f,
            -1.0f, -1f,   0f, 1.0f,
            1.0f, -1.0f,   1f, 1.0f,
            1.0f,  1.0f,   1f, 0.0f,
            -1.0f,  1.0f,   0f, 0.0f,
            -1.0f, -1.0f,   0f, 1.0f
};
    float color[] = {
            0.0f, 1.0f, 0.0f ,
            1.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 1.0f,
            0.0f, 1.0f, 0.0f ,
            1.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 1.0f
    };
    
    
    private FloatBuffer tableBuffer;
    private FloatBuffer colorBuffer;
    
//    public Table(ColorShaderProgram colorShaderProgram) {
//        tableBuffer= ByteBufferUtils.makeFloteByteBuffer(VERTEX_DATA);
//        colorBuffer=ByteBufferUtils.makeFloteByteBuffer(color);
//        this.colorShaderProgram=colorShaderProgram;
//        bindData(colorShaderProgram);
//    }
public Table(TexureShaderProgram texureShaderProgram) {
    tableBuffer= ByteBufferUtils.makeFloteByteBuffer(VERTEX_DATA);
    colorBuffer=ByteBufferUtils.makeFloteByteBuffer(color);
    this.texureShaderProgram=texureShaderProgram;
    bindData(texureShaderProgram);
}
    public void bindData(TexureShaderProgram texureShaderProgram) {
        GLES20.glEnableVertexAttribArray(texureShaderProgram.aPosition);
        tableBuffer.position(0);
        GLES20.glVertexAttribPointer(texureShaderProgram.aPosition,
                2,GLES20.GL_FLOAT,
                false,16,tableBuffer);
        tableBuffer.position(2);
        GLES20.glEnableVertexAttribArray(texureShaderProgram.aTextureCoordinates);
        GLES20.glVertexAttribPointer(texureShaderProgram.aTextureCoordinates,
                2,GLES20.GL_FLOAT,
                false,16,tableBuffer);

    }


    //    public void bindData(ColorShaderProgram colorShaderProgram) {
//        GLES20.glEnableVertexAttribArray(colorShaderProgram.aPosition);
//        tableBuffer.position(0);
//        GLES20.glVertexAttribPointer(colorShaderProgram.aPosition,
//                2,GLES20.GL_FLOAT,
//                false,0,tableBuffer);
//        tableBuffer.position(0);
//        GLES20.glEnableVertexAttribArray(colorShaderProgram.aColor);
//        GLES20.glVertexAttribPointer(colorShaderProgram.aColor,
//                3,GLES20.GL_FLOAT,
//                false,0,colorBuffer);
//        
//    }
//    
    public void draw() {
        glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);
    }
    public void setUniformMatrix(float[] matrix){
       // GLES20.glUniformMatrix4fv(colorShaderProgram.uMatrix,1,false,matrix,0);
        
    }
}
